package com.chenfan.mcn.enums;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 业务类型枚举
 *
 * @author wulg
 * @date 2021-10-19
 **/
@SuppressWarnings({"AlibabaEnumConstantsMustHaveComment"})
@AllArgsConstructor
public enum BusinessTypeEnum {
    CUSTOMER(1, "客户"),
    STAR_AGENT(2, "红人-经纪人"),
    STAR_EXECUTOR(15, "红人-执行人"),
    DISCOUNT_APPROVAL(3, "折扣返点审批单"),
    YEAR_FRAME_CONTRACT(4, "年框合同"),
    INCOME_CONTRACT(5, "收入合同"),
    EXCUTION_ORDER(6, "执行单"),
    PURCHASE_CONTRACT(7, "采购合同"),
    ANNUAL_REBATE(8, "客户返点审批单"),
    CHARGE(9, "费用管理"),
    TAX(10, "开票管理"),
    CLEAR(11, "核销管理"),
    INVOICE(12, "账单管理"),
    BANK_AND_CASH(13, "实收付"),
    STAR_REBATE_CONTRACT(14, "红人返点合同"),
    ;

    /**
     * 类型
     */
    @Getter
    private final Integer type;

    /**
     * 名称
     */
    @Getter
    private final String name;

    /**
     * 根据type获取对应枚举
     * @param type
     * @return
     */
    public static BusinessTypeEnum toEnum(Integer type) {
        for (BusinessTypeEnum item : BusinessTypeEnum.values()) {
            if (item.type.equals(type)) {
                return item;
            }
        }
        return null;
    }

    /**
     * 根据type获取name
     * @param type
     * @return
     */
    public static String getBusinessTypeName(Integer type) {
        BusinessTypeEnum item = toEnum(type);
        return item != null ? item.name : null;
    }

    /**
     * List集合是否存在非法类型
     * @param typeList
     * @return
     */
    public static boolean existIllegalType(List<Integer> typeList) {
        List<Integer> tempList = new ArrayList<>(typeList.size());
        try {
            tempList.addAll(typeList);
            for (BusinessTypeEnum item : BusinessTypeEnum.values()) {
                tempList.remove(item.getType());
            }
            return tempList.size() > 0;
        } finally {
            tempList.clear();
        }
    }

    /**
     * 筛选财务的业务类型
     * @param businessTypeList
     * @return
     */
    public static List<Integer> filterFinance(List<Integer> businessTypeList) {
        List<Integer> tempList = new ArrayList<>(5);
        if(CollectionUtils.isNotEmpty(businessTypeList)) {
            if(businessTypeList.contains(BusinessTypeEnum.CHARGE.type)) {
                tempList.add(BusinessTypeEnum.CHARGE.type);
            }
            if(businessTypeList.contains(BusinessTypeEnum.TAX.type)) {
                tempList.add(BusinessTypeEnum.TAX.type);
            }
            if(businessTypeList.contains(BusinessTypeEnum.CLEAR.type)) {
                tempList.add(BusinessTypeEnum.CLEAR.type);
            }
            if(businessTypeList.contains(BusinessTypeEnum.INVOICE.type)) {
                tempList.add(BusinessTypeEnum.INVOICE.type);
            }
            if(businessTypeList.contains(BusinessTypeEnum.BANK_AND_CASH.type)) {
                tempList.add(BusinessTypeEnum.BANK_AND_CASH.type);
            }
        }
        return tempList;
    }
    /**
     * 筛选BaseInfo的业务类型
     * @param businessTypeList
     * @return
     */
    public static List<Integer> filterBaseInfo(List<Integer> businessTypeList) {
        List<Integer> tempList = new ArrayList<>(1);
        if(CollectionUtils.isNotEmpty(businessTypeList)) {
            if(businessTypeList.contains(BusinessTypeEnum.CUSTOMER.type)) {
                tempList.add(BusinessTypeEnum.CUSTOMER.type);
            }
        }
        return tempList;
    }

    /**
     * 拆解业务类型type并拼接业务类型Name
     * @param businessTypes
     * @return
     */
    public static String joinBusinessTypeName(String businessTypes) {
        List<Integer> businessTypeList = JSONObject.parseObject(businessTypes, new TypeReference<List<Integer>>(){});
        if(CollectionUtils.isNotEmpty(businessTypeList)) {
            StringBuffer sb = new StringBuffer();
            Iterator<Integer> iterator = businessTypeList.iterator();
            while(iterator.hasNext()) {
                String businessTypeName = BusinessTypeEnum.getBusinessTypeName(iterator.next());
                if(StringUtils.isBlank(businessTypeName)) {
                    continue;
                }
                sb.append(businessTypeName);
                if(iterator.hasNext()) {
                    sb.append(",");
                }
            }
            return sb.toString();
        }
        return null;
    }
}
